﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using Microsoft.Phone.Info;

namespace BlogTruyen
{
    public partial class Reading3 : PhoneApplicationPage
    {
        ViewModels.Reading Data;
        const string current = "ApplicationCurrentMemoryUsage";
        DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 5) };
        public Reading3()
        {
            InitializeComponent();
            Data = new ViewModels.Reading();
            DataContext = Data;
            timer.Tick += timer_Tick;
        }


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!Data.isLoaded)
            {
                Data.chapter_name = NavigationContext.QueryString["title"];
                var webClient = API.GetWebClient();
                webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
                webClient.DownloadStringAsync(new Uri(string.Format("http://blogtruyen.com{0}", NavigationContext.QueryString["url"]), UriKind.Absolute));
                busyIndicator.IsRunning = true;
            }
        }
        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (API.CheckWebClient(e))
            {
                Data.LoadHtml(e.Result);
                busyIndicator.IsRunning = false;
                timer.Start();
            }
        }

        private void controlBar_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            controlBar.Visibility = Visibility.Collapsed;
        }

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (controlBar.Visibility == Visibility.Collapsed)
            {
                controlBar.Visibility = Visibility.Visible;
                timer.Start();
            }
            else
            {
                controlBar.Visibility = Visibility.Collapsed;
            }
        }
        void timer_Tick(object sender, EventArgs e)
        {
            controlBar.Visibility = Visibility.Collapsed;
            timer.Stop();
        }

        private void RadImageButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if ((this.Orientation & PageOrientation.Landscape) == 0)
            {
                this.SupportedOrientations = SupportedPageOrientation.Landscape;
            }
            else
            {
                this.SupportedOrientations = SupportedPageOrientation.Portrait;
            }
        }

        private void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            var img = ((FrameworkElement)sender).DataContext as ViewModels.Img;
            img.isLoading = false;
            //Data.LoadNextImage(img.id);
            if (Debugger.IsAttached)
            {
                
                double currentBytes = ((long)DeviceExtendedProperties.GetValue(current)) / 1024.0 / 1024.0;
                Debugger.Log(0,"RAM",String.Format("RAM={0}\n",currentBytes));
            }
        }
        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            //if ((this.Orientation & PageOrientation.Landscape) == 0)
            //{
            //    btnRotate.RestStateImageSource = new BitmapImage(new Uri("/Assets/Icons/dark/appbar.transform.rotate.right.png", UriKind.Relative));
            //}
            //else
            //{
            //    btnRotate.RestStateImageSource = new BitmapImage(new Uri("/Assets/Icons/dark/appbar.transform.rotate.left.png", UriKind.Relative));
            //}
        }
    }
}